<template>
  <div class="login">
    <div class="login-content layout-flex">
      <el-form :class="userAgent ? 'wid-70' : 'wid-25'">
        <h1 class="tac mb30">2217后台管理系统</h1>
        <el-form-item>
          <el-input v-model="user.username" class="wid-100" prefix-icon="icon aliiconfont el-icon-aliicon_signal" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="user.password"
            type="password"
            show-password
            prefix-icon="icon aliiconfont el-icon-alilock"
            class="wid-100"
            placeholder="密码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="wid-100">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'login',
  data() {
    return {
      user: {
        username: '',
        password: '',
      },
    };
  },
  methods: {
    login() {
      if (!this.user.username) {
        this.$notify({
          title: '警告',
          message: '用户名不能为空',
          type: 'warning',
        });
        return false;
      }
      if (!this.user.password) {
        this.$notify({
          title: '警告',
          message: '密码不能为空',
          type: 'warning',
        });
        return false;
      }
      this.$store
        .dispatch('userInfo', this.user)
        .then(res => {
          // 失败在这里处理 成功在store中处理
          if (String(res.code) === '-1') {
            this.$notify({
              title: '警告',
              message: res.msg,
              type: 'warning',
            });
          } else {
            this.$router.replace('/');
          }
        })
        .catch(() => {
          this.$notify({
            title: '警告',
            message: '账号密码错误，或者已经停用',
            type: 'warning',
          });
        });
      //存token
      this.$store
        .dispatch('token', this.user)
        .then(res => {
          // 失败在这里处理 成功在store中处理
          if (String(res.code) == '-1') {
            this.$notify({
              title: '警告',
              message: res.msg,
              type: 'warning',
            });
          } else {
            this.$router.replace('/');
          }
        })
        .catch(() => {
          this.$notify({
            title: '警告',
            message: '账号密码错误，或者已经停用',
            type: 'warning',
          });
        });
      // api.login(this.user).then((res) => {
      //   if (res.code == "0") {
      //     localStorage.setItem("uid", res.data.realname);
      //     localStorage.setItem("isLogined", 1);
      //     //提交 mutation 更改vuex用户信息
      //     this.$store.commit("GET_USER_INFO", res);
      //     this.$store.commit("IS_LOGINED", 1);
      //     //跳转到首页
      //     this.$router.push("/index");
      //   } else {
      //     localStorage.setItem("isLogined", 0);
      //     this.$store.commit("IS_LOGINED", 0);
      //   }
      // });
    },
  },
  created() {},
};
</script>
<style lang="stylus" scoped>
.login {
  height: 100%;

  .login-content {
    height: 100%;
    justify-content: center;
    align-items: center;
    margin-top: -10vh;
  }
}

.align {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
</style>
